/*
 厦门大学计算机专业 | 前华为工程师
 专注《零基础学编程系列》  http://lblbc.cn/blog
 包含：Java | 安卓 | 前端 | Flutter | iOS | 小程序 | 鸿蒙
 公众号：蓝不蓝编程
 */
Page({
  data: {
    array: [2, 1, 5, 4, 3],
    arrayStr: '',
  },
  onLoad: function () {
    this.shuffle()
  },
  shuffle() {
    let array = [2, 1, 5, 4, 3]
    this.setData({
      array: array,
      arrayStr: this.convertToStr(array),
    })
  },
  sort() {
    let array = this.data.array
    var min = array[0]
    var max = min
    for (var i = 0; i < array.length; i++) {
      if (array[i] > max) {
        max = array[i]
      } else if (array[i] < min) {
        min = array[i]
      }
    }

    var counting = Array(max - min + 1).fill(0)
    for (var i = 0; i < array.length; i++) {
      counting[array[i] - min] += 1
    }
    var index = -1
    for (var i = 0; i < counting.length; i++) {
      for (var j = 0; j < counting[i]; j++) {
        index++
        array[index] = i + min
      }
    }
    this.setData({
      array: array,
      arrayStr: this.convertToStr(array),
    })
  },
  convertToStr(array: number[]) {
    var result = ""
    for (let i = 0; i < array.length; i++) {
      result += array[i] + " "
    }
    return result;
  }

})